const router = require('koa-router')()
const UserHelp =require('../database/users/userHelp')
const VerifyUtils=require('../utils/VerifyUtils')



//注册聊天用户
router.post('/api/login',async (ctx, next) => {

    let {pic,username,uid,appVersion} = ctx.request.body

    //验证参数
    VerifyUtils.tips(ctx,{pic},{username},{uid},)

    let data=await UserHelp.login(pic,username,uid,appVersion)

    ctx.body = {
        code: 0,
        data: data
    }

})



//获取用户信息
router.get('/api/getUserInfo',async (ctx, next) => {

    let uid = ctx.request.query.uid-0
    VerifyUtils.tips(ctx,{uid})

    let data = await UserHelp.getUserInfo(uid)

    if(data.code==1){
        ctx.body = data
    }
    ctx.body = {
        code: 0,
        data: data
    }
})




//该用户是否存在
router.get('/api/isUserExist', async (ctx, next) => {
    let uid=ctx.request.query.uid

    let data= await UserHelp.isUserExist(uid-0)
    ctx.body={
        code:0,
        data:data
    }
})



//更新用户头像
router.post('/api/updateUser', async (ctx, next) => {
    let avatar=ctx.request.body.avatar
    let uid=ctx.request.body.uid
    VerifyUtils.tips(ctx,{uid},{avatar})
    let data= await UserHelp.updateUser(uid,avatar)
    ctx.body={
        code:0,
        data:data
    }
})




//以下接口只用来调试，不供前端使用
//获取聊天用户列表
router.get('/api/userlist', async (ctx, next) => {

    let data= await UserHelp.getUserList()
    ctx.body={
        code:0,
        data:data
    }
})



module.exports = router
